/*
   Playing around in C++
   Implementing a simple "point and shoot" game
   @Author: George Elias Ferreira da Silva
   @Date: 10/14/2011
*/

#include <iostream>
#include <stdlib.h>
#include <math.h>
#include <time.h>

// Definitions
#define VELOCITY 200.0 // initial velocity of 200 ft/sec
#define GRAVITY 32.2 // gravity for distance calculation
#define MIN_DISTANCE 300
#define MAX_DISTANCE 900
#define kPi 3.1415

int numEnemies = 0;
double enemyDistance = 0;

// Funtcion to randomize enemy distance
double Distance() {
	std::cout << "The enemy is " << enemyDistance << " meters away." << std::endl;
	enemyDistance = rand() % MAX_DISTANCE + MIN_DISTANCE;
	return enemyDistance;
}

// Function used to handle "shots"
int Shoot(double _angle) {
	_angle = (_angle * kPi) / 180.0;
	double time_in_air = (2.0 * VELOCITY * sin(_angle)) / GRAVITY;
	double shotDistance = round((VELOCITY * cos(_angle)) * time_in_air);
	if (shotDistance == enemyDistance)
		return 0;
	else
		return enemyDistance - shotDistance;
}

// Function used to handle players input and the game's logic
int Fire() {
	double angle;
	enemyDistance = Distance();

	for (int i = 0; i <= 10; i++) {
		std::cout << "What angle?" << std::endl;
		std::cin >> angle;
		double shotResult = Shoot(angle);
		if ( shotResult == 0) {
			std::cout << "You destroyed it!" << std::endl;
			return 1;
		}
		else {
			std::cout << "You " << ((shotResult > 0) ? "under " : "over ") << "shot by" << abs(shotResult) << std::endl;
		}
	}
	return 0;
}

// Function that handles starting up the game and prints some info on screen
void StartUp() {
	std::cout << "Welcome to Artillery." << std::endl;
	std::cout << "You are in the middle of a war and being charged by thousands of enemies." << std::endl;
	std::cout << "You have one cannon, which you can shoot at any angle." << std::endl;
	std::cout << "You only have 10 cannonballs for each target.." << std::endl;
	std::cout << "Let's begin..." << std::endl;
	
	// Initialize random seed.
	srand (time(NULL));
	// Generates a random numer of enemies between 1 and 30
	numEnemies = rand() % 30 + 1;
}

int main (int argv, char * argc[]) {

	StartUp();
	int killed = 0;
	char done;
	
	do {
		if (Fire())
			killed++;
		std::cout << "I see another one, care to shoot again? (Y/N)" << std::endl;
		std::cin >> done;
	} while (done != 'n');
	
	std::cout << "You killed " << killed << " of the enemy." << std::endl;
}